package com.net.oil.coupon;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.base.BaseActivity;
import com.net.oil.coupon.bean.CouponRecordRefBusiness;
import com.net.oil.coupon.bean.CouponRecordVO;
import com.net.oil.coupon.bean.OilBusinessDTO;
import com.net.oil.coupon.contract.CouponDetailContract;
import com.net.oil.coupon.presenter.CouponDetailPresenter;
import com.net.oil.gaode.RoutePlanActivity;
import com.net.oil.http.AppUrl;
import com.net.oil.util.DateFormat;
import com.net.oil.util.glide.GlideUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CouponDetailActivity extends BaseActivity<CouponDetailPresenter> implements CouponDetailContract.View {


    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.amount)
    TextView amount;
    @BindView(R.id.use_amount_tv)
    TextView useAmountTv;
    @BindView(R.id.coupon_title)
    TextView couponTitle;
    @BindView(R.id.get_time)
    TextView getTime;
    @BindView(R.id.coupon_status)
    TextView couponStatus;
    @BindView(R.id.use_time)
    TextView useTime;
    @BindView(R.id.coupon_code)
    TextView couponCode;
    @BindView(R.id.use_restrict_tv)
    TextView useRestrictTv;
    @BindView(R.id.use_shop_tv)
    TextView useShopTv;
    @BindView(R.id.shop_logo)
    ImageView shopLogo;
    @BindView(R.id.take_me_to_layout)
    LinearLayout takeMeToLayout;
    @BindView(R.id.shop_name)
    TextView shopName;
    @BindView(R.id.shop_address)
    TextView shopAddress;
    @BindView(R.id.shop_info_layout)
    LinearLayout shopInfoLayout;
    @BindView(R.id.coupon_code_layout)
    LinearLayout couponCodeLayout;
    @BindView(R.id.coupon_bg_layout)
    FrameLayout couponBgLayout;
    @BindView(R.id.use_time_layout)
    LinearLayout useTimeLayout;
    private int id = -1;

    private CouponRecordVO couponDetailBean;
    private OilBusinessDTO businessDTO;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon_detail);
        ButterKnife.bind(this);
        presenter = new CouponDetailPresenter(this);
        initData();
        initView();

    }

    private void initData() {
        id = getIntent().getIntExtra(Constant.INTENT_COUPON_ID_KEY, -1);
        if (id != -1) {
            presenter.getCouponDetail(id);
        }
    }

    private void initView() {
        toolbarTitle.setText("优惠券详情");
    }

    /**
     * 获取优惠券成功
     *
     * @param couponDetailBean 优惠券bean
     */
    @Override
    public void getCouponDetailSuccess(CouponRecordVO couponDetailBean) {
        this.couponDetailBean = couponDetailBean;
        if (couponDetailBean != null) {

            /* 获取优惠券时间 */
            if (couponDetailBean.getApplyTime() != null) {
                getTime.setText(DateFormat.formatDateTime(couponDetailBean.getApplyTime(), DateFormat.FormatFlag.YDMHMS));
            }
            /* 优惠券title */
            if (!TextUtils.isEmpty(couponDetailBean.getCouponName())) {
                couponTitle.setText(couponDetailBean.getCouponName());
            } else {
                couponTitle.setText("优惠券");
            }

            /* 券码 */
            if (!TextUtils.isEmpty(couponDetailBean.getCode())) {
                couponCode.setText(couponDetailBean.getCode());
            }

            /* 优惠券金额 */
            if (couponDetailBean.getAmount() != null) {
                amount.setText(String.format(getString(R.string.money_format), String.valueOf(couponDetailBean.getAmount())));
            }

            /* 满多少可以使用 */
            if (couponDetailBean.getFullAvailableAmount() != null) {
                useAmountTv.setText(String.format(getString(R.string.coupon_use_format), String.valueOf(couponDetailBean.getFullAvailableAmount())));
            }

            /* 使用规则 */
            if (!TextUtils.isEmpty(couponDetailBean.getUsedRule())) {
                useRestrictTv.setText(couponDetailBean.getUsedRule());
            }
            /* 可使用的商家 */
            List<CouponRecordRefBusiness> list = couponDetailBean.getCouponRecordRefBusinessList();
            if (list != null) {
                String names = null;
                for (CouponRecordRefBusiness business : list) {
                    if (!TextUtils.isEmpty(business.getBusinessName())) {
                        if (TextUtils.isEmpty(names)) {
                            names = business.getBusinessName();
                        } else {
                            names = names + business.getBusinessName() + "，";
                        }
                    }
                }
                useShopTv.setText(names);
            } else {
                useShopTv.setText("全部商家可用");
            }

            //used状态代表是否使用，expireStatus 代表是否过期（1：过期 0：未过期）
            if (couponDetailBean.getUsed() != null && couponDetailBean.getUsed()) {
                couponStatus.setText("已使用");
                couponCodeLayout.setVisibility(View.GONE);
                couponStatus.setTextColor(getResources().getColor(R.color.text_grey));
                couponBgLayout.setBackground(getResources().getDrawable(R.drawable.coupon_bg_use));
                String userTime = DateFormat.formatDateTime(couponDetailBean.getUsedTime(), DateFormat.FormatFlag.YDMHMS);
                useTime.setText(userTime);
                setShopView(couponDetailBean);

            } else {
                shopInfoLayout.setVisibility(View.GONE);
                useTimeLayout.setVisibility(View.GONE);
                if (couponDetailBean.getExpireStatus() == 1) {
                    couponStatus.setText("已过期");
                    couponCodeLayout.setVisibility(View.GONE);
                    couponStatus.setTextColor(getResources().getColor(R.color.text_grey));
                    couponBgLayout.setBackground(getResources().getDrawable(R.drawable.coupon_bg_use));
                } else {
                    couponStatus.setText("未使用");
                    couponStatus.setTextColor(getResources().getColor(R.color.white));
                    amount.setTextColor(getResources().getColor(R.color.white));
                    couponStatus.setTextColor(getResources().getColor(R.color.default_color));
                }
            }

        }
    }

    private void setShopView(CouponRecordVO couponDetailBean) {
        businessDTO = couponDetailBean.getBusiness();
        if (businessDTO != null) {
            shopInfoLayout.setVisibility(View.VISIBLE);
            /* 商家logo */
            GlideUtil.getInstance().loadCircleImage(this, shopLogo, AppUrl.getImageUrl(businessDTO.getLogo()), R.drawable.shop_err);

            /* 商家名称 */
            if (!TextUtils.isEmpty(businessDTO.getName())) {
                shopName.setText(businessDTO.getName());
            }

            /* 商家地址 */
            if (!TextUtils.isEmpty(businessDTO.getAddress())) {
                shopAddress.setText(businessDTO.getAddress());
            }
        }

    }

    /**
     * 获取优惠券失败
     */
    @Override
    public void getCouponDetailFailed() {

    }

    @OnClick({R.id.toolbar_go_back, R.id.take_me_to_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_go_back:
                finish();
                break;
            case R.id.take_me_to_layout:
                Intent intent = new Intent(this, RoutePlanActivity.class);
                if (businessDTO != null && businessDTO.getLat() != null && businessDTO.getLng() != null) {
                    intent.putExtra(Constant.INTENT_LNG, businessDTO.getLng());
                    intent.putExtra(Constant.INTENT_LAT, businessDTO.getLat());
                }
                startActivity(intent);
        }
    }

}
